<?php

namespace App\Http\Controllers\Api\V1;

use App\Enums\ResponseCode;
use App\Enums\SmsEnum;
use App\Exceptions\ApiExceptions;
use App\Exceptions\BusinessException;
use App\Http\Controllers\Api\AbstractApiController;
use App\Http\Resources\UserInfo;
use App\Http\Resources\UserResumeInfo;
use App\Models\User;
use App\Services\Sms\SmsService;
use App\Services\User\EducationService;
use App\Services\User\UserService;
use App\Services\User\WorkExperienceService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

class UserController extends AbstractApiController
{
    /**
     * @param Request $request
     * @return JsonResponse
     */
    public function updateResume(Request $request): JsonResponse
    {
        $input = $request->validate([
            'name' => 'required|max:5',
            'gender' => 'required|in:' . implode(',', array_keys(User::GENDER_LIST)),
            'birthday' => 'required|date',
            'identity' => 'in:' . implode(',', array_keys(User::IDENTITY_LIST)),
        ]);
        UserService::editPayProfile(auth('api')->id(), $input);
        return $this->success(trans("修改个人简历成功"));
    }

    /**
     * 个人简历信息
     * @return JsonResponse
     */
    public function resumeInfo(): JsonResponse
    {
        $info = auth('api')->user();
        return self::success(trans("success"), ResponseCode::OK, new UserResumeInfo($info));
    }
    /**
     * 修改个人描述
     * @param Request $request
     * @return JsonResponse
     */
    public function updateDescription(Request $request): JsonResponse
    {
        $input = $request->validate([
            'description' => 'required|max:150',
        ]);
        UserService::editPayProfile(auth('api')->id(), [
            'self_description' => $input['description'],
        ]);
        return $this->success(trans("修改个人描述成功"));
    }

    /**
     * 修改手机号
     * @param Request $request
     * @return JsonResponse
     * @throws BusinessException
     */
    public function editMobile(Request $request): JsonResponse
    {
        $input = $request->validate([
            'mobile' => 'required',
            'verify_code' => 'required',
        ]);

        SmsService::verify($input['mobile'], $input['verify_code'], SmsEnum::CHANGE_MOBILE_OLD);
        //验证安全密码
        UserService::editPayProfile(auth('api')->id(), ['mobile' => $input['mobile']]);
        return self::success(trans("绑定手机号码成功"));
    }

    /**
     * 修改教育经历
     * @param Request $request
     * @param EducationService $service
     * @return JsonResponse
     * @throws ApiExceptions
     */
    public function updateEducation(Request $request, EducationService $service): JsonResponse
    {
        $input = $request->validate([
            'school_name' => 'required:max:20',
            'degree' => 'required|max:10',
            'graduation_date' => 'required|date',
            'major' => 'max:20',
        ]);
        $service->update(
            uid: auth('api')->id(),
            name: $input['school_name'],
            degree: $input['degree'],
            graduation_date: $input['graduation_date'],
            major: $input['major']??'',
        );
        return self::success(trans("修改教育经历成功"));
    }

    //添加工作经历
    public function addWorkExperience(Request $request,WorkExperienceService $service){
        $input = $request->validate([
            'company_name' => 'required:max:20',
            'start_date' => 'required|date',
            'end_date' => 'required|date',
            'job_description' => 'required:max:150',
        ]);
        $service->create(
            uid: auth('api')->id(),
            company_name: $input['company_name'],
            start_date: $input['start_date'],
            end_date: $input['end_date'],
            job_description: $input['job_description'],
        );
        return self::success(trans("添加工作经历成功"));
    }


    /**
     * 修改工作经历
     * @param Request $request
     * @param WorkExperienceService $service
     * @return JsonResponse
     * @throws ApiExceptions
     */
    public function updateWorkExperience(Request $request,WorkExperienceService $service): JsonResponse
    {
        $input = $request->validate([
            'company_name' => 'required:max:20',
            'start_date' => 'required|date',
            'end_date' => 'required|date',
            'job_description' => 'required:max:150',
            'work_experience_id' => 'required'
        ]);
        $service->update(
            uid: auth('api')->id(),
            work_experience_id: $input['work_experience_id'],
            company_name: $input['company_name'],
            start_date: $input['start_date'],
            end_date: $input['end_date'],
            job_description: $input['job_description'],
        );
        return self::success(trans("修改工作经历成功"));
    }


    //删除工作经历
    public function delWorkExperience(Request $request,WorkExperienceService $service)
    {
        $input = $request->validate([
            'work_experience_id' => 'required'
        ]);
        $service->delete(
            work_experience_id: $input['work_experience_id'],
            uid: auth('api')->id(),
        );
        return self::success(trans("工作经历删除成功"));

    }

    /**
     *
     *
     * 更新头像
     * @param Request $request
     * @return JsonResponse
     * @throws BusinessException
     */
    public function updateAvatar(Request $request): JsonResponse
    {
        $file = $request->file('image');
        if (!empty($file)) {
            $storage = Storage::disk();
            $uploadFile = $storage->put(SystemController::$dictionaries[1], $file);
            if (!$uploadFile) {
                self::error(trans("上传文件失败"));
                self::exception(ResponseCode::SERVICE_UNAVAILABLE, trans(''));
            }
        } else {
            self::error(trans("请选择文件"));
        }
        UserService::editPayProfile(auth('api')->id(), [
            'avatar_url' => $storage->url($uploadFile),
        ]);
        return self::success(trans("修改工作经历成功"),ResponseCode::OK,new UserInfo(User::query()->find(auth('api')->id())));
    }
}
